package com.css.fxfzysx.modules.vabStructure.repository.impl;

import com.css.fxfzysx.modules.cztDataMange.entity.CbcbInfoEntity;
import com.css.fxfzysx.modules.cztDataMange.entity.CbcpInfoEntity;
import com.css.fxfzysx.modules.cztDataMange.entity.CpvAnalysisEntity;
import com.css.fxfzysx.modules.cztDataMange.entity.CpvInfoEntity;
import com.css.fxfzysx.modules.vabStructure.entity.CztBrickConcreteDestroyCalc;
import com.css.fxfzysx.modules.vabStructure.entity.CztBrickConcreteDestroyResult;
import com.css.fxfzysx.modules.vabStructure.repository.PobStructureRepository;
import com.css.fxfzysx.util.CreateGson;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.web.PlatformSessionContext;
import com.css.fxfzysx.zcpt.sys.entity.SUser;
import com.css.fxfzysx.zcpt.sys.service.SUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class PobStructureRepositoryImpl implements PobStructureRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Autowired
    private SUserService userService;

    @Override
    public void saveBrickConcreteDestroyCalc(CztBrickConcreteDestroyCalc entity) {
        jdbcTemplate.update("insert into czt_brick_concrete_destroy_calc  (cbcdc_id,cs,qtkjcd,dzjlzsxs,resistance_force,d6,d7,d8,d9,d10,degree6,degree7,degree8,degree9,degree10,create_user,create_time,del_flag,project_id ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                entity.getCbcdcId(), entity.getCs(), entity.getQtkjcd(), entity.getDzjlzsxs(), entity.getResistanceForce(), entity.getD6(), entity.getD7(), entity.getD8(), entity.getD9(), entity.getD10(), entity.getDegree6(), entity.getDegree7(), entity.getDegree8(), entity.getDegree9(), entity.getDegree10(), entity.getCreateUser(), entity.getCreateTime(), entity.getDelFlag(), entity.getProjectId());
    }

    @Override
    public void saveBrickConcreteDestroyResults(CztBrickConcreteDestroyResult entity) {
        jdbcTemplate.update("insert into czt_brick_concrete_destroy_result  (cbcdr_id,break_state,conclusion,create_user,create_time,del_flag,project_id,json_data1,json_data2,break_state_sort ) values (?,?,?,?,?,?,?,cast(? as jsonb),cast(? as jsonb),?)",
                entity.getCbcdrId(), entity.getBreakState(), entity.getConclusion(), entity.getCreateUser(), entity.getCreateTime(), entity.getDelFlag(), entity.getProjectId(), entity.getJsonData1(), entity.getJsonData2(),
                entity.getBreakStateSort());
    }

    @Override
    public int delByProjectId1(List<String> idList) {
        Map<String, Object> args = new HashMap<>();
        args.put("idList", idList);
        return namedParameterJdbcTemplate.update("delete from czt_brick_concrete_destroy_calc where project_id in (:idList)",
                args);
    }

    @Override
    public int delByProjectId2(List<String> idList) {
        Map<String, Object> args = new HashMap<>();
        args.put("idList", idList);
        return namedParameterJdbcTemplate.update("delete from czt_brick_concrete_destroy_result where project_id in (:idList)", args);
    }

    @Override
    public List<CztBrickConcreteDestroyCalc> getByProjectId1(String projectId) {
        return jdbcTemplate.query("select * from czt_brick_concrete_destroy_calc where project_id = '" + projectId + "'",
                new BeanPropertyRowMapper<>(CztBrickConcreteDestroyCalc.class));
    }

    @Override
    public List<CztBrickConcreteDestroyResult> getByProjectId2(String projectId) {
        return jdbcTemplate.query("select * from czt_brick_concrete_destroy_result where project_id = '" + projectId + "' order by break_state_sort asc",
                new BeanPropertyRowMapper<>(CztBrickConcreteDestroyResult.class));
    }

    @Override
    public HashMap<String, Object> queryPobStructure(CbcbInfoEntity cbcbInfoEntity, int page, int size) {
        StringBuilder sql = new StringBuilder("select *,(province || '-' || city || '-' || county) region," +
                "(structure_type || '-' || structure_type1) st,(purpose || '-' || purpose1) as use " +
                " from czt_brick_concrete_building_info where del_flag='0'");
        if ("1".equals(cbcbInfoEntity.getRole())) {
            sql.append(" and create_user='" + PlatformSessionContext.getUserID() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcbInfoEntity.getCreateStartTime())) {
            sql.append(" and create_time >= '" + cbcbInfoEntity.getCreateStartTime() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcbInfoEntity.getCreateEndTime())) {
            String endTime = cbcbInfoEntity.getCreateEndTime() + " 23:59:59";
            sql.append(" and create_time <='" + endTime + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcbInfoEntity.getProvince())) {
            sql.append(" and province ='" + cbcbInfoEntity.getProvince() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcbInfoEntity.getCity())) {
            sql.append(" and city ='" + cbcbInfoEntity.getCity() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcbInfoEntity.getCounty())) {
            sql.append(" and county ='" + cbcbInfoEntity.getCounty() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcbInfoEntity.getStructureType())) {
            sql.append(" and structure_type ='" + cbcbInfoEntity.getStructureType() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcbInfoEntity.getStructureType1())) {
            sql.append(" and structure_type1 ='" + cbcbInfoEntity.getStructureType1() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcbInfoEntity.getPurpose())) {
            sql.append(" and purpose ='" + cbcbInfoEntity.getPurpose() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcbInfoEntity.getPurpose1())) {
            sql.append(" and purpose1 ='" + cbcbInfoEntity.getPurpose1() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcbInfoEntity.getFortification())) {
            sql.append(" and fortification ='" + cbcbInfoEntity.getFortification() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcbInfoEntity.getYears())) {
            sql.append(" and years ='" + cbcbInfoEntity.getYears() + "'");
        }
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CbcbInfoEntity.class)).size();
        sql.append(" order by create_time desc").append(" limit " + size + " offset " + (page - 1) * size);
        List<CbcbInfoEntity> data = jdbcTemplate.query(sql.toString(),
                new BeanPropertyRowMapper<>(CbcbInfoEntity.class));
        HashMap<String, Object> resultMap = new HashMap();
        resultMap.put("total", total);
        resultMap.put("rows", data);
        return resultMap;
    }
    @Override
    public HashMap<String, Object> queryPobStructure1(CbcpInfoEntity cbcpInfoEntity, int page, int size) {
        StringBuilder sql = new StringBuilder("select * from czt_brick_concrete_param_info where del_flag='0'");
        if (cbcpInfoEntity.getRole().equals("1")) {
            sql.append(" and create_user='" + PlatformSessionContext.getUserID() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcpInfoEntity.getCreateStartTime())) {
            sql.append(" and create_time >= '" + cbcpInfoEntity.getCreateStartTime() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcpInfoEntity.getCreateEndTime())) {
            String endTime = cbcpInfoEntity.getCreateEndTime() + " 23:59:59";
            sql.append(" and create_time <='" + endTime + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcpInfoEntity.getProvince())) {
            sql.append(" and province ='" + cbcpInfoEntity.getProvince() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcpInfoEntity.getCity())) {
            sql.append(" and city ='" + cbcpInfoEntity.getCity() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcpInfoEntity.getCounty())) {
            sql.append(" and county ='" + cbcpInfoEntity.getCounty() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcpInfoEntity.getStructureType())) {
            sql.append(" and structure_type ='" + cbcpInfoEntity.getStructureType() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcpInfoEntity.getStructureType1())) {
            sql.append(" and structure_type1 ='" + cbcpInfoEntity.getStructureType1() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcpInfoEntity.getPurpose())) {
            sql.append(" and purpose ='" + cbcpInfoEntity.getPurpose() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcpInfoEntity.getPurpose1())) {
            sql.append(" and purpose1 ='" + cbcpInfoEntity.getPurpose1() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcpInfoEntity.getFortification())) {
            sql.append(" and fortification ='" + cbcpInfoEntity.getFortification() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cbcpInfoEntity.getYears())) {
            sql.append(" and years ='" + cbcpInfoEntity.getYears() + "'");
        }
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CbcpInfoEntity.class)).size();
        StringBuilder orderSql = new StringBuilder("order by create_time desc");
        StringBuilder limitSql = new StringBuilder(" limit " + size + " offset " + (page - 1) * size);
        sql.append(orderSql).append(limitSql);
        List<CbcpInfoEntity> data = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CbcpInfoEntity.class));
        for (CbcpInfoEntity datum : data) {
            if (PlatformObjectUtils.isNotEmpty(datum.getCreateUser())) {
                String user = userService.getSUser(datum.getCreateUser());
                SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
                datum.setUserName(sUser.getUserName());
            }
            if (PlatformObjectUtils.isNotEmpty(datum.getProvince()) && PlatformObjectUtils.isNotEmpty(datum.getCity()) && PlatformObjectUtils.isNotEmpty(datum.getCounty())) {
                StringBuilder region = new StringBuilder(datum.getProvince()).append("-").append(datum.getCity()).append("-").append(datum.getCounty());
                datum.setRegion(region.toString());
            }
            if (PlatformObjectUtils.isNotEmpty(datum.getStructureType())) {
                StringBuilder st = new StringBuilder(datum.getStructureType());
                if(PlatformObjectUtils.isNotEmpty(datum.getStructureType1())){
                    st.append("-"+datum.getStructureType1());
                }
                datum.setSt(st.toString());
            }
        }
        HashMap<String, Object> resultMap = new HashMap();
        resultMap.put("total", total);
        resultMap.put("rows", data);
        return resultMap;
    }

    @Override
    public void updateCountStatusById(String id) {
//        String sql = "update czt_brick_concrete_building_info set calculate_or_not = '0' where cbcbi_id like " +
//                "'%(select cbcbi_id from czt_brick_resistance_main where correction_coefficient = " + id + "')%'";
        String sql = "update czt_brick_concrete_building_info set calculate_or_not = '0' where correction_coefficient like '%"+id+"%'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void moveBasicLibraryById(String dataId, String operate) {
        String sql = "update czt_brick_concrete_building_info set basic_library= ? where cbcbi_id = ?";
        jdbcTemplate.update(sql, operate, dataId);
    }
    @Override
    public void moveBasicLibraryById1(String dataId, String operate) {
        String sql = "update czt_brick_concrete_param_info set basic_library= ? where cbcpi_id = ?";
        jdbcTemplate.update(sql, operate, dataId);
    }

    @Override
    public void updateCalculateOrNotStatusById(String cbcbiId) {
        String sql = "update czt_brick_concrete_building_info set calculate_or_not = '1' where cbcbi_id = ?";
        jdbcTemplate.update(sql, cbcbiId);
    }
}
